package md.lanjing.com.module_mine.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.lxh.baselibray.base.XFragment;
import com.lxh.baselibray.constant.ARouterConfig;
import com.lxh.baselibray.util.ARouterUtils;
import com.lxh.baselibray.util.ObjectUtils;
import com.lxh.baselibray.util.SPUtils;
import com.lxh.baselibray.util.ToastUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;
import md.lanjing.com.module_mine.R;
import md.lanjing.com.module_mine.R2;
import md.lanjing.com.module_mine.ui.activity.AccountSetActivity;
import md.lanjing.com.module_mine.ui.activity.BindPurseAddressActivity;
import md.lanjing.com.module_mine.ui.activity.ContactUsActivity;
import md.lanjing.com.module_mine.ui.activity.InviteCodeActivity;
import md.lanjing.com.module_mine.ui.activity.RealNameAuthenticationActivity;
import md.lanjing.com.module_mine.ui.activity.ReceiveMoneyTypeActivity;


import com.lxh.baselibray.constant.Constant;
import com.lxh.baselibray.event.EventImpl;



/**
 * 个人中心
 */
public class PersonCenterFragment extends XFragment {


    @BindView(R2.id.tv_real_right)
    ImageView tvRealRight;
    @BindView(R2.id.tv_real_flag)
    TextView tvRealFlag;
    @BindView(R2.id.tv_tel)
    TextView tvTel;

    String realName;

    @Override
    public void initData(Bundle savedInstanceState) {
        realName = SPUtils.getString(Constant.IS_REAL, null, context);
        if (ObjectUtils.isEmpty(realName)) {
            //未认证
            tvRealRight.setVisibility(View.VISIBLE);
            tvRealFlag.setVisibility(View.GONE);
        } else {
            //已认证
            tvRealRight.setVisibility(View.GONE);
            tvRealFlag.setVisibility(View.VISIBLE);
        }
        String phone = SPUtils.getString(Constant.PHONE, null, context);
        if (!ObjectUtils.isEmpty(phone)){
            tvTel.setText(phone.substring(0,3)+"****"+phone.substring(phone.length()-4,phone.length()));
        }

    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_person_center;
    }




    @OnClick({R2.id.tv_invite_code, R2.id.relative_authenticated, R2.id.tv_set_account, R2.id.tv_contact_us,R2.id.tv_receive_type,R2.id.tv_purse_address})
    public void onViewClicked(View view) {
        int i = view.getId();
        if (i == R.id.tv_invite_code) {
            startActivity(new Intent(context, InviteCodeActivity.class));

        } else if (i == R.id.relative_authenticated) {
            if (ObjectUtils.isEmpty(realName)) {//已经实名认证的情况下，就不需要跳转了
                startActivity(new Intent(context, RealNameAuthenticationActivity.class));
            } else {
                ToastUtils.showLongToast(context, "已实名认证");
            }

        } else if (i == R.id.tv_set_account) {
            startActivity(new Intent(context, AccountSetActivity.class));
        } else if (i == R.id.tv_contact_us) {
            startActivity(new Intent(context, ContactUsActivity.class));
        } else if (i == R.id.tv_receive_type) {
            startActivity(new Intent(context, ReceiveMoneyTypeActivity.class));

        } else if (i == R.id.tv_purse_address) {
            startActivity(new Intent(context, BindPurseAddressActivity.class));
        }
    }
    @Override
    public boolean useEventBus() {
        return true;
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void realReceive(EventImpl.RealEvent event) {
        if (!ObjectUtils.isEmpty(event)) {
            tvRealRight.setVisibility(View.GONE);
            tvRealFlag.setVisibility(View.VISIBLE);
            realName = SPUtils.getString(Constant.IS_REAL, null, context);
        }
    }
}
